package guda.demox.web.controller;

import guda.demox.biz.BlogBiz;
import guda.demox.biz.common.BizResult;
import guda.demox.biz.common.BizResultHelper;
import guda.demox.biz.common.ErrorCode;
import guda.demox.biz.common.ErrorMessageHandler;
import guda.demox.biz.form.BlogForm;
import guda.demox.common.validator.BeanValidator;
import guda.demox.common.validator.ValidResult;
import guda.demox.dal.common.PageQuery;
import guda.demox.web.security.AuthUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by well on 2017/11/8.
 */
@RestController
public class BlogController {

    @Resource
    private BlogBiz blogBiz;
    @Resource
    private ErrorMessageHandler errorMessageHandler;

    @RequestMapping("/blog/{id}")
    public BizResult detail(@PathVariable Long id) {
        throw new RuntimeException("dafd");
        //return blogBiz.detail(id);
    }

    @RequestMapping("/blog/list")
    BizResult list(HttpServletRequest request) {
        //request.getSession().setAttribute("uid","zhangsan");
        System.out.println(AuthUser.getCurrentUser().getLoginName());
        return blogBiz.list(new PageQuery());
    }

    @RequestMapping(value = "/blog/doCreate" ,method = RequestMethod.POST)
    BizResult doCreate(HttpServletRequest request, @RequestBody  BlogForm blogForm) {
        ValidResult valid = BeanValidator.valid(blogForm);
        if(valid.hasError()){
            return BizResultHelper.newFail(ErrorCode.PARAM_ERROR);
        }
        return BizResultHelper.newSuccess();
    }

    @RequestMapping("/blog/noAuth")
    BizResult noAuth(HttpServletRequest request) {
        //request.getSession().setAttribute("uid","zhangsan");
        System.out.println("no auth");
        System.out.println(request.getSession().getAttribute("uid"));
        return blogBiz.list(new PageQuery());
    }
}
